package org.varscape.core.util;

import java.util.ArrayList;
import java.util.StringTokenizer;

// Breaks a line to several pieces.
// This class is extensively used and should be optimized for performance.
public class Tokenizer {

	// Tokenize a string using Tab
	public static ArrayList<String> tabTokenize(String str) {
		return tokenize(str, "\t");
	}
	
	// Tokenize a string using <delim> and parse each string to integer
	public static ArrayList<Integer> tokenizeToInt(String str, String delim) {
		ArrayList<String> list = tokenize(str, delim);
		ArrayList<Integer> returnList = new ArrayList<Integer>();
		for (int i = 0; i < list.size(); i++) {
			returnList.add(Integer.parseInt(list.get(i)));
		}
		return returnList;
	}
	
	// Tokenize a string using <delim>	
	public static ArrayList<String> tokenize(String str, String delim) {
		ArrayList<String> result = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(str, delim, true);
		String lastToken = "", currToken = "";
		
		while (st.hasMoreTokens()) {
			currToken = st.nextToken();
			// if the current token is the delimiter, then put the last token in the result list
			if (currToken.equals(delim)) {
				result.add(lastToken);
				lastToken = "";
			} else {
				lastToken += currToken;
			}
		}
		// flush the last token
		if (! ("".equals(lastToken))) {
			result.add(lastToken);
		}
		
		return result;
	}
}